<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../res/css/common/font-awesome.min.css" media="all">
		<link rel="stylesheet" href="../../res/layui-config/css/app.css" media="all">
		<title>采购增加页面(原材料)</title>
    </script>
	</head>
	
	<script type="text/html" id="input">
	<div class="layui-inline">
		<input class="layui-input" class="oness" maxlength="5" style="border:none;padding-bottom:8px;" name="matAmount"  autocomplete="off"  value="{{d.matAmount}}" id="number{{d.matId}}" onkeyup="Pressdown(this);">
	</div>
	</script>
	
	<script type="text/html" id="inputss">
	<div class="layui-inline">
		<input class="layui-input layui-disabled"  readonly="readonly"  value="0" maxlength="9" style="border:none;padding-bottom:8px;" name="bdetTotal" id="price{{d.matId}}" autocomplete="off"">
	</div>
	</script>
	
	<body>
		<div>
			<input type="hidden" id="shuju" style="width: 100%">
		</div>
		<div class="kit-table">
				<!-- 左上角的两个按钮 -->
		        <div class="kit-table-header">
		        
		        <form class="layui-form" lay-filter="kit-search-form">
		        <div class="kit-search-inputs" style="margin-left: 200px;">
		                <div class="kit-search-keyword">
		                    <label class="layui-form-label"><span class="color-red"><span class="color-red"></span>供应商:</label>
							  <div class="layui-input-inline" style="float:right;">
							    <select id="edit_exam_school" name="buyCompany">
							      <option value="">请选择</option>
							    </select>
							  </div>
		                </div>
		            </div>
		        </form>
					<div class="layui-form-item">
					   <label class="layui-form-label">采购总数量</label>
					   <div class="layui-input-inline">
					     <input type="text" name="buyAmount" value="0" id="buyAmount" autocomplete="off" class="layui-input layui-disabled" readonly="readonly">
					   </div>
					   <label class="layui-form-label">采购总金额</label>
					   <div class="layui-input-inline">
					     <input type="text" name="buyMoney" value="0" id="buyMoney" autocomplete="off" class="layui-input layui-disabled" readonly="readonly">
					   </div>
					</div>
					
				
		            <div class="kit-search-btns" style="width: 100px;margin-left: 630px;margin-top: -60px;">
		             <a class="layui-btn layui-btn-sm" id="add"><i class="layui-icon"></i> 保存订单</a>
		        	</div>
		        	<div class="kit-search-btns" style="width: 100px;margin-left: 720px;margin-top: -60px;">
		             <a class="layui-btn layui-btn-sm" id="fh"><i class="layui-icon"></i> 返回</a>
		        	</div>
		        </div>
			
		    
		    
			<!-- 操作按钮 -->
		    <div class="kit-table-body">
		        <table id="demo" lay-filter="demo"></table>
		        <script type="text/html" id="barDemo">
		            <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
		            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
		            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		        </script>
		    </div>
		</div>
		
		
		<script src="../../res/js/common/jquery-1.11.2.min.js"></script>
		<script>
			//文本框只能输入金额
		    function amount(th){
		        var regStrs = [
		            ['^0(\\d+)$', '$1'], //禁止录入整数部分两位以上，但首位为0
		            ['[^\\d\\.]+$', ''], //禁止录入任何非数字和点
		            ['\\.(\\d?)\\.+', '.$1'], //禁止录入两个以上的点
		            ['^(\\d+\\.\\d{2}).+', '$1'] //禁止录入小数点后两位以上
		        ];
		        for(i=0; i<regStrs.length; i++){
		            var reg = new RegExp(regStrs[i][0]);
		            th.value = th.value.replace(reg, regStrs[i][1]);
		        }
		    }
			//搜索框同步输入
			function lockInput(obj,id){
				$("#"+id).val($(obj).val());
			}
		</script>
		<script src="../../res/layui/layui.all.js"></script>
		<script>
			window.windata=null;
			//显示表格数据
		    layui.use(['table','upload',], function() {
		        var table = layui.table,
		            $ = layui.jquery,
		            layer = layui.layer,
		            form = layui.form,
		            laytpl = layui.laytpl;
		        var tableElem;
		        var tableIns = table.render({
		            elem: '#demo',
		            height: autoHeight(), //容器高度
		            url: '../../material/showList.action',
		            page: true,
		            id: 'demo',
		            cols: [
		                [{checkbox: true,fixed: true},
		                {field: 'matId',title: 'ID',width: 120},	
		                {field: 'matName',title: '材料名',width: 120},
		                {field: 'matPrice',title: '单价',width: 120},
		                {field: 'matAmount',title: '数量',width: 120,templet:'#input'},
		                {title: '小计',width: 150,templet:'#inputss'}]
		            ],
		            done: function(res, curr, count) {
		            	windata=res.data;
		            	tableElem = this.elem.next('.layui-table-view');
		            	
		            	 var purchaseTexts = new Array();
				            purchaseTexts=$("#shuju").val().split("#");
					       	 for (var i = 0; i < purchaseTexts.length-1; i++) {
					       		$('#demo').next().find('.layui-table-body tr').each(function(index,item){
					       			var ids=$(item).find('td[data-field="matId"]').text();
					       			if(purchaseTexts[i].split(",")[0]==ids){
					       				var index=$(item).data('index');
					       	            var che = $('#demo').next().find('.layui-table-body tr[data-index="'+index+'"] input[name="layTableCheckbox"]')
					       	            che.prop("checked",true)
					       	           form.render();
					       			}
					       			
					       		});
					       	}
		            	
		            },
		            loading: true
		        });
		        
				//查询类型下拉框数据
		        $.ajax({ url:"../../provider/showList.action",
                    type:'post',//method请求方式，get或者post
                    cache: false,//同步
                    dataType:'json',//预期服务器返回的数据类型
                    success:function(mes){//res为相应体,function为回调函数
                    	//循环添加框内的数据
                    	$.each(mes.data,function(index,item){
                    		$("#edit_exam_school").append("<option value="+item.provId+">"+item.provName+"</option>");
                    	});
                    	//渲染框内的下拉框
                    	form.render();
                    }
                 });
		        
		       
		       
				
		        
		        //监听搜索表单提交
		        form.on('submit(search)', function(data) {  
		            //带条件查询
		            tableIns.reload({
		                where: data.field,
		                page: { curr: 1 }
		            });
		            $('.kit-search-mored').hide();
		            return false;
		        });
		        
		        
	        	//选中、取消值
	        	var data=null;
		        table.on('checkbox(demo)', function(obj){
		        	var tables=table.cache.demo;
		        	data=obj.data;
		        	var dd = tableElem.find('tr[data-index="' + data.LAY_TABLE_INDEX + '"]').find('td[data-field="matAmount"] input').val();
		        	if(dd>0){
		        	if(obj.checked==true){
		        		 $("#shuju").val($("#shuju").val()+data.matId+","+data.matName+","+data.matPrice+","+$("#number"+data.matId).val()+","+$("#price"+data.matId).val()+"#");
		        		 
		        		 $.each(tables,function(i,item){
		        			 if(data.matId==item.matId){
		        				 //取输入数量值
		        				 var a=$("#number"+data.matId).val();
		        				 //放入总数量
		        				 $("#buyAmount").val(parseInt($("#buyAmount").val())+parseInt(a));
		        				 
		        				 //取输入小计值
		        				 var b=$("#price"+data.matId).val();
		        				 //放入总金额
		        				 $("#buyMoney").val(parseInt($("#buyMoney").val())+parseInt(b));
		        			 }
		        		 });
		        		 
		        	 }	
		        	 if(obj.checked==false){
		        		 deleteAsu(data);
		        		 $.each(tables,function(i,item){
		        			 if(data.matId==item.matId){
		        				 //获取总数量值
		        				 var zAmount=$("#buyAmount").val();
	        					 //用总数量值减去输入的值
		        				 $("#buyAmount").val(parseInt(zAmount)-$("#number"+data.matId).val());
	        					 
		        				 //获取总金额值
		        				 var zMoney=$("#buyMoney").val();
	        					 //用总金额减去输入的小计值
		        				 $("#buyMoney").val(parseInt(zMoney)-$("#price"+data.matId).val());
		        			 }
		        		 });
		        	 }
		        	}else{
		        		layer.msg("请先输入数量");
		        		var cheTr=$("#number"+data.matId).parents("tr")
		        		var index=$(cheTr).data('index');
	       	            var che = $('#demo').next().find('.layui-table-body tr[data-index="'+index+'"] input[name="layTableCheckbox"]')
		        		che.prop("checked",false)
		        		form.render();
		        	}
		        });
		        
		        $("#add").click(function(){
		        	//0代表加载的风格，支持0-2
		        	//loading层
		        	var index = layer.load(1, {
		        	  shade: [0.8,'#fff'] //0.1透明度的白色背景
		        	});

   	        		var url="../../buyht/add.action";
   					var data={"shuju":$("#shuju").val(),"sumNumber":$("#buyAmount").val(),"sumPrice":$("#buyMoney").val(),"Supplier":$("#edit_exam_school").val()}
   					
   					if($("#edit_exam_school").val()!=0 && $("#buyAmount").val()!=0 ){
   						$.post(url,data,function(mes){
  	 						if(mes.status==1){
  	 							
  	 							location.href="purchase.html";
  	 						}
  	 					})
   					}else{
   						layer.msg('请选择供应商和数据噢', {
   							     time: 1500, //1.5s后自动关闭
   							 });
   						layer.close(index);
   					}
   				})
		        
		        
		        //表单渲染
		        form.render(null, 'kit-search-form');
		        //更多筛选点击事件
		        $('#kit-search-more').on('click', function() {
		            $('.kit-search-mored').toggle();
		        });
		        var editIndex;
		    });
			
		  	//窗口自适应
			function autoHeight() {
				var h = document.body.clientHeight && document.documentElement.clientHeight - 50; //窗口高度
				//监听改变窗口大小
				window.onresize = function() {
					h = document.body.clientHeight && document.documentElement.clientHeight - 50; //窗口高度
					tableIns.reload({
						height: h
					});
				}
				return h;
			}
			
			
		  	//全屏弹窗
			function maxlayer(){
				var index = layer.open({
				  type: 2,
				  title: '药品配置',
				  content: 'https://www.baidu.com',
				  area: ['800px', '430px']
				  //maxmin: true
				});
				layer.full(index);
			}
		  	
		  	//重新渲染表单
		  	function renderForm(){ 
	  			layui.use('form', function(){ 
		  			var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加() 
		  			form.render(); 
	  			}); 
	  		}
		    
		</script>
		<script>
			$(function(){
				$("#fh").click(function(){
					location.href="purchase.html";
				})
			})
		</script>
		
		
		<script>
			

			function  deleteAsu(data){
				var purchaseTexts = new Array();
				 var purchaseTexts=$("#shuju").val().split("#");
				 $("#shuju").val("")
				 for (var i = 0; i < shuju.length-1; i++) {
					if(purchaseTexts[i].indexOf(data.matId)!=0){
						$("#shuju").val($("#shuju").val()+(shuju[i]+"#"))
					} 
				}
			}
		
		
			function Pressdown(obj){
				obj.value = obj.value.replace(/[^\d]/g,"");//清除“数字”和“.”以外的字符  
			    if(obj.value ==0){
			    	obj.value = "0";
			    }
			    if(obj.value.indexOf(".")< 0 && obj.value !=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额 
			        obj.value= parseFloat(obj.value); 
			    }
			    if(obj.value.indexOf(".")==0){//第一个字符不能为.
			    	 obj.value="0";
			    }
			    
			    var id = obj.id;
			    
				var a = obj.value;//数量值
				var b=$(obj).parent().parent().parent().prev().children().html();//获取this的父类节点
				var c =parseFloat(a)*parseFloat(b);//用this*数量
				var d =c.toFixed(1);
				//计算结果显示
				$(obj).parent().parent().parent().next().children().children().children().val(d);
			}
			
			function load() {
				var form =layui.form;
				form.render();
			}
		</script>
		
	</body>
</html>
